import Router from 'koa-router';
import {
  User,
  sequelize,
} from '../models';
import {
  checkLogined,
  createLoginToken,
  createSecretPair,
  checkSecretPair,
} from '../modules/user';
import * as types from '../types';
import StatusError from "../utils/StatusError";
import { sendCode, verifyCode } from '../modules/mobile';
import objectFilter from 'just-filter';

const router = new Router();
export default router;

router.post('/login', types.checkBody({
  mobile: types.mobile.required,
  pwd: types.password.required,
}), async ctx => {
  const { mobile, pwd } = ctx.request.body;
  const user = await User.findOne({
    where: { mobile },
    attributes: ['id', 'salt', 'secret'],
  });
  if (user === null || !checkSecretPair(pwd, user)) {
    throw StatusError.FORBIDDEN;
  }
  ctx.body = {
    ok: 1,
    token: createLoginToken(user.id),
  };
});

router.put('/me',checkLogined,types.checkBody({
  name:types.name,
  avatar:types.assetsKey,
}),async ctx => {
  const { uid } = ctx.session;
  const { name, avatar } = ctx.request.body;
  await User.update(
    objectFilter({ name, avatar }, (k,v) => v !== undefined),
    { where: { id: uid } }
  );
  ctx.body = {
    ok:1,
  };
});

router.get('/me',checkLogined,async(ctx) => {
  const {uid} = ctx.session;
  const user = await User.findById(uid,{
    attributes:[
      'id','name','mobile','avatar'
    ],
  });
  if (!user){
    throw StatusError.UNAUTHORIZED;
  }
  ctx.body = user.get({plain:true});
})

router.post('/pwd/change',checkLogined,types.checkBody({
  oldPwd:types.password.required,
  pwd:types.password.required,
}),async ctx => {
  const {uid} = ctx.session;
  const {pwd,oldPwd} = ctx.request.body;
  const user = await User.findById(uid,{
    attributes:['id','salt','secret'],
  });
  if(!checkSecretPair(oldPwd,user)){
    throw StatusError.FORBIDDEN;
  }
  const {salt,secret} = createSecretPair(pwd);
  await user.update({
    salt,
    secret,
  });
});

router.post('/pwd/reset',types.checkBody({
  mobile:types.mobile.required,
  pwd:types.password.required,
  verifiCode:types.verifiCode.required,
}),async ctx =>{
  const { mobile, pwd, verifiCode } = ctx.request.body;
  await verifyCode(mobile, verifiCode);
  const user = await User.findOne({
    where:{mobile},
    attributes:['id','mobile'],
  });
  if(!user){
    throw StatusError.NOT_FOUND;
  }
  const {salt,secret} = createSecretPair(pwd);
  await user.update({
    salt,
    secret,
  });
});

router.post('/register',types.checkBody({
  mobile:types.mobile.required,
  pwd:types.password.required,
  name: types.name.required,
  verifiCode: types.verifiCode.required,
}),async ctx => {
  const { mobile, name, pwd, verifiCode } = ctx.request.body;
  await verifyCode(mobile, verifiCode);
  const {salt, secret} = createSecretPair(pwd);
  const user = await User.create({name, mobile, salt, secret});

  ctx.body = {
    ok: 1,
    token: createLoginToken(user.id),
  };
});

router.post('/sendCode', types.checkBody({
  mobile:types.mobile.required,
}), sendCode);
